/*
 * @Author: NJ_1098 xiabaoxin
 * @Date: 2025-02-11 14:15:09
 * @LastEditors: NJ_1098 xiabaoxin
 * @LastEditTime: 2025-02-17 13:11:19
 * @Description: 用户服务层
 */
const moment = require("moment");
const { Op } = require("sequelize");
const { UUID_LENGTH, UUID_RADIX } = require("../config/config.default");
const CommonUtils = require("../utils/common.js");
const UserModel = require("../model/user.model.js");

class UserService {
  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 新增用户
   * @param {*} account
   * @param {*} password
   * @param {*} openid
   * @param {*} wname
   * @param {*} avatar
   * @return {*}
   */
  static async add({ account, password, openid, wname, avatar }) {
    try {
      const lastOpt = {};
      account && Object.assign(lastOpt, { account });
      password && Object.assign(lastOpt, { password });
      openid && Object.assign(lastOpt, { openid });
      wname && Object.assign(lastOpt, { wname });
      avatar && Object.assign(lastOpt, { avatar });
      const uid = CommonUtils.UUID(UUID_LENGTH, UUID_RADIX);
      const name = wname ? wname : account;
      Object.assign(lastOpt, { uid });
      Object.assign(lastOpt, { name });
      const res = await UserModel.create(lastOpt);
      return res.dataValues;
    } catch (err) {
      throw new Error(err);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 删除用户
   * @param {*} uids
   * @return {*}
   */
  static async delete(uids) {
    try {
      const res = await UserModel.destroy({
        where: {
          uid: {
            [Op.in]: uids,
          },
        },
      });
      let msg = "";
      if (res > 0) {
        msg = `成功删除${res}条数据`;
      } else {
        msg = "未找到删除数据";
      }
      return msg;
    } catch (err) {
      throw new Error(err);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 获取用户信息
   * @param {*} account 账号
   * @param {*} password 密码
   * @param {*} uid 用户id
   * @param {*} openid 用户在微信保留id
   * @param {*} fields 返回字段列表
   * @param {*} status 查询的用户状态列表
   * @return {*}
   */
  static async getUserInfo({ account, password, uid, openid }, fields, status) {
    try {
      const lastOpt = {};
      let customFields = fields ? fields : ["uid", "openid", "account"];
      let lastStatus = status ? status : ["01", "03"];
      account && Object.assign(lastOpt, { account });
      password && Object.assign(lastOpt, { password });
      uid && Object.assign(lastOpt, { uid });
      openid && Object.assign(lastOpt, { openid });
      const res = await UserModel.findOne({
        attributes: customFields,
        where: {
          ...lastOpt,
          status: {
            [Op.in]: lastStatus,
          },
        },
      });
      return res ? res.dataValues : null;
    } catch (err) {
      throw new Error(err);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 更新用户信息
   * @param {*} uid
   * @param {*} password
   * @param {*} name
   * @param {*} avatar
   * @param {*} role
   * @param {*} status
   * @param {*} reason
   * @return {*}
   */
  static async update(uid, { password, name, avatar, role, status, reason }) {
    try {
      const updateData = {};
      password && Object.assign(updateData, { password });
      name && Object.assign(updateData, { name });
      avatar && Object.assign(updateData, { avatar });
      role && Object.assign(updateData, { role });
      status && Object.assign(updateData, { status });
      reason && Object.assign(updateData, { reason });
      const [res] = await UserModel.update(updateData, {
        where: { uid },
      });
      return res;
    } catch (err) {
      throw new Error(err);
    }
  }
}

module.exports = UserService;
